﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Vega.USiteBuilder;
using SPCFMedicineInformer.DocumentTypes;
using System.Data;
using umbraco.cms.businesslogic.web;
using umbraco;
using SPCFMedicineInformer.Constants;
using System.IO;

namespace SPCFMedicineInformer.Masterpages
{
    public partial class Admin_Slideshow : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindGrid();
                BindDropdown();
            }
        }
        void BindGrid()
        {
            var list = NodeLocator.GetNodesOfExactType<SlideshowItem>().OrderBy(a => a.Id);
            rptSlideshows.DataSource = list;
            rptSlideshows.DataBind();
        }
        protected void rpt_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                SlideshowItem slide = (SlideshowItem)e.Item.DataItem;

                Literal litContent = (Literal)e.Item.FindControl("litContent");
                litContent.Text = Helpers.StringUtils.TruncateIfLonger(slide.Content,80);

                Literal litSlideTitle = (Literal)e.Item.FindControl("litSlideTitle");
                litSlideTitle.Text = Helpers.StringUtils.TruncateIfLonger(slide.SlideTitle, 40);

                Literal litShowinFrontpage = (Literal)e.Item.FindControl("litShowinFrontpage");
                litShowinFrontpage.Text = slide.ShowInFrontpage ? "Yes" : "No";
            }
        }
        void Delete(int id)
        {
            if (id > 0)
            {
                Document doc = new Document(id);
                if (doc.Published)
                {
                    DeleteImage(doc.getProperty(UmbracoPropertyName.IMAGE_URL).Value.ToString());
                    doc.UnPublish();
                    library.UnPublishSingleNode(doc.Id);
                }
                doc.delete();
                BindGrid();
            }
        }
        void DeleteImage(string src)
        {
            string path = Server.MapPath(src);
            var file = new FileInfo(path);
            file.Delete();
        }
        protected void repeater_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            int intTemp = 0;
            if (int.TryParse((string)e.CommandArgument, out intTemp))
            {
                switch (e.CommandName.ToLower())
                {
                    case "delete":
                        Delete(intTemp);
                        break;
                }
            }
        }
        protected void btnAddNewSlide_Click(object sender, EventArgs e)
        {
            Response.Redirect("/admin/utilities/slideshows/slideshow-entry");
        }
        void BindDropdown()
        {
            ddlTitles.Items.Add(new ListItem("Id", "Id"));
            ddlTitles.Items.Add(new ListItem("Title", "Title"));
        }
        protected void btnFilter_Click(object sender, EventArgs e)
        {
            IEnumerable<SlideshowItem> list = NodeLocator.GetNodesOfExactType<SlideshowItem>().Where(a => a.SlideTitle.ToLower().Contains(txtSearch.Text.ToLower()));
            if (cbShowInFrontpage.Checked)
                list = list.Where(a => a.ShowInFrontpage == cbShowInFrontpage.Checked);

            if (rdoSort.Text == "Asc")
            {
                switch (ddlTitles.SelectedValue)
                {
                    case "Id":
                        list = list.OrderBy(a => a.Id);
                        break;
                    case "Title":
                        list = list.OrderBy(a => a.SlideTitle);
                        break;
                }
            }
            else
            {
                switch (ddlTitles.SelectedValue)
                {
                    case "Id":
                        list = list.OrderByDescending(a => a.Id);
                        break;
                    case "Title":
                        list = list.OrderByDescending(a => a.SlideTitle);
                        break;
                }
            }
            rptSlideshows.DataSource = list;
            rptSlideshows.DataBind();

        }
    }
}